<?php
namespace App\Http\Controllers\Admin;

use App\Service\Admin\CaseComService;
use Illuminate\Http\Request;

class CaseComController extends BaseController
{
    //用户

    public function __construct(CaseComService $service)
    {
        $this->db = $service;
    }

    //设计公司列表
    public function getIndex(Request $request)
    {
        //header("Access-Control-Allow-Origin: *");
        $pages['start'] = $request->post('start', 1);
        $pages['limit'] = $request->post('limit', 10);
        $res = $this->db->getIndex($pages);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data'], $res['total']);
    }

    //修改
    public function modify(Request $request)
    {
        //header("Access-Control-Allow-Origin: *");
        $id = $request->post('id', 0);
        $pas['cname'] = $request->post('cname', '');
        $pas['logo'] = $request->post('img', '');
        $pas['address'] = $request->post('address', '');
        $pas['type'] = 3; //设计公司固定3
        if (!$pas['cname']) {
            return $this->toJson(-2, '项目名称必须的');
        }
        if ($id) {
            $res = $this->db->modify($id, $pas);
        } else {
            $res = $this->db->add($pas);
        }
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    //删除
    public function del(Request $request)
    {
        //header("Access-Control-Allow-Origin: *");
        $id = $request->post('id', 0);
        $res = $this->db->del($id);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }
}